package top.yuki.data;

import org.bukkit.Bukkit;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Player;
import org.yaml.snakeyaml.Yaml;
import top.yuki.Chat.ChatCharacteristic;
import top.yuki.LevelWorld;

import java.io.*;
import java.util.*;


public class DataManager {
    public HashMap<String,HashMap>  dataMap;
    private LevelWorld lw;
    private HashMap user_data = null;
    private File dataFold;
    private ConfigurationSection config;
    private WorldManage worldManage;


    public static DataManager init(LevelWorld lw) {

        return new DataManager(lw);
    }          		//简单生成对象的方法

    public DataManager(LevelWorld lw) {
        this.lw = lw;
        this.dataMap = loadData();
        this.config = lw.getConfig();
        this.worldManage = lw.worldManage;

    }

    public HashMap loadData() {
        dataFold = lw.getDataFolder();
        File dataFile = new File(dataFold+File.separator+"data.yml");
        Yaml yml = new Yaml();
        try {
            InputStream reader = new FileInputStream(dataFile);
            HashMap map = yml.loadAs(reader, HashMap.class);
            if (map==null){
                return new HashMap();
            }
            return map;

        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return null;
        }



    }


    public void writeData(UUID uuid, String name, int level, int essence,int worldLevel){


        HashMap map = new HashMap();
        map.put("name",name);
        map.put("level",level);
        map.put("essence",essence);
        map.put("worldLevel",worldLevel);
        map.put("xp",0);
        map.put("showBoard","true");

        dataMap.put(String.valueOf(uuid),map);
    }
    public void save(){
        dataFold = lw.getDataFolder();
        File dataFile = new File(dataFold+File.separator+"data.yml");
        Yaml yaml = new Yaml();
        try {
            FileWriter writer = new FileWriter(dataFile);
            yaml.dump(dataMap,writer);
        } catch (IOException e) {
            e.printStackTrace();
        }



    }

    public int getLevel(UUID uuid){
        String id = uuid.toString();
        user_data = dataMap.get(id);
        int level = (int) user_data.get("level");

        return level;
    }
    public int getEssence(UUID uuid){
        String id = uuid.toString();
        user_data = dataMap.get(id);
        int Essence = (int) user_data.get("essence");
        return Essence;
    }
    public int getWorldLevel(UUID uuid){
        String id = uuid.toString();
        user_data = dataMap.get(id);
        int worldLevel = (int) user_data.get("worldLevel");
        return worldLevel;
    }
    public int getXp(UUID uuid){
        String id = uuid.toString();
        user_data = dataMap.get(id);
        int Essence = (int) user_data.get("xp");
        return Essence;
    }
    public boolean addXp(UUID uuid,int add){
        String id = uuid.toString();
        int xp = getXp(uuid);
        int maxXp = calMaxXp(uuid);
        if (xp+add >= maxXp) {
            int overflow = xp+add -maxXp;
            setLevel(uuid,getLevel(uuid)+1);
            addEssence(uuid,calMaxEssence(uuid));
            tryUpdateWorldLevel(uuid,getLevel(uuid));
            user_data.replace("xp",overflow);
        }else{
            user_data = dataMap.get(id);
            user_data.replace("xp",xp+add);
        }
        return true;
    }

    public void tryUpdateWorldLevel(UUID uuid,int level){
        Set<String> upgradeWorldLv = config.getConfigurationSection("upgradeWorldLv").getKeys(true);
        HashMap<Integer, String> upgrade = new HashMap();
        Player player = Bukkit.getPlayer(uuid);
        for (String s : upgradeWorldLv) {
            int key = (int) config.get("upgradeWorldLv." + s);
            upgrade.put(key,s);

        }
        if (upgrade.containsKey(level)) {
            setWorldLevel(uuid, Integer.parseInt(String.valueOf(upgrade.get(level))) );
            player.sendMessage(ChatCharacteristic.SYSTEM_CAT.system+"已升级世界等级");

        }



    }
    public boolean setLevel(UUID uuid,int newLevel){
        String id = uuid.toString();
        dataMap.get(id).replace("level",newLevel);

        return true;
    }

    public boolean setEssence(UUID uuid,int newEssence){
        String id = uuid.toString();
        dataMap.get(id).replace("essence",newEssence);
        return true;
    }
    public boolean setWorldLevel(UUID uuid,int newWorldLevel){
        String id = uuid.toString();
        dataMap.get(id).replace("worldLevel",newWorldLevel);
        return true;
    }
    public boolean addEssence(UUID uuid,int add){
        String id = uuid.toString();
        int readEssence = getEssence(uuid);

        dataMap.get(id).replace("essence",readEssence+add);
        return true;
    }
    public boolean addWorldLevel(UUID uuid,int add){
        String id = uuid.toString();
        int readWorldLevel = getWorldLevel(uuid);
        dataMap.get(id).replace("essence",readWorldLevel+add);
        return true;
    }

    public int calMaxEssence(UUID uuid){
        int level = getLevel(uuid);
        int Max = 0;
        Max = 8/9*level+120;
        return Max;
    }
    public int calMaxXp(UUID uuid){

        int level = getLevel(uuid);
        int Max = 0;
        if (level<=10 ){
            Max = 10000;
        }else{
            Max = 1000*level;
        }
        return Max;
    }
    public int calMaxMinecraftLevel(UUID uuid){

        int Max = 0;
        Max = (int) config.get("minecraftLevel." + getWorldLevel(uuid));
        return Max;
    }


    public boolean  initPlayer(Player p ){
        String id = p.getUniqueId().toString();
        if (!dataMap.containsKey(id)){
            writeData(p.getUniqueId(),p.getName(),1,120,1);
        }


        return true;
    }


}
